package com.taomus.mytools.lang.utils.asm;

import java.io.PrintWriter;

import org.objectweb.asm.ClassWriter;
import org.objectweb.asm.Opcodes;
import org.objectweb.asm.Type;
import org.objectweb.asm.commons.GeneratorAdapter;
import org.objectweb.asm.commons.Method;
import org.objectweb.asm.util.TraceClassVisitor;

public class AsmUtils extends ClassLoader {

	private ClassWriter cwriter = null;
	private TraceClassVisitor tcvisitor = null;

	public AsmUtils() {
		cwriter = new ClassWriter(ClassWriter.COMPUTE_MAXS);
		tcvisitor = new TraceClassVisitor(cwriter, new PrintWriter(System.err));
	}

	public CreateClass createClass(String name) {
		return new CreateClass(tcvisitor, name);
	}

	public Class<?> loadClass2(String name) {
		tcvisitor.visitEnd();
		byte[] bytes = cwriter.toByteArray();
		System.out.println(bytes.length);
		Class<?> cl = this.defineClass(name, bytes, 0, bytes.length);
		return cl;
	}

}
